<template> 
  <div>
    <el-upload
      :action="useQiniu?qiniuUploadUrl:minioUploadUrl"
      :data="useQiniu?dataObj:null"
      :list-type="listType"
      :multiple="false" :show-file-list="showFileList"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过10MB</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="fileList[0].url" alt="">
    </el-dialog>
  </div>
</template>
<script>
  import {token} from '@/api/qiniu'

  export default {
    name: 'singleUploadQiniu',
    props: {
      value: String,
      listType: {
        type:String,
        default:'picture-card'
      }
    },
    computed: {
      imageUrl() {
        return this.value;
      },
      imageName() {
        if (this.value != null && this.value !== '') {
          return this.value.substr(this.value.lastIndexOf("/") + 1);
        } else {
          return null;
        }
      },
      fileList() {
        return [{
          name: this.imageName,
          url: this.imageUrl
        }]
      },
      showFileList: {
        get: function () {
          return this.value !== null && this.value !== ''&& this.value!==undefined;
        },
        set: function (newValue) {
        }
      }
    },
    data() {
      return {
        dataObj: {
          token: '',
          serverUrl:'',
          bucketname:''
        },
        dialogVisible: false,
        useQiniu:true, //使用qiniu->true;使用MinIO->false
        qiniuUploadUrl:'https://up-z0.qiniup.com',
        minioUploadUrl:'http://localhost:8080/minio/upload',
      };
    },
    methods: {
      emitInput(val) {
        this.$emit('input', val)
      },
      handleRemove(file, fileList) {
        this.emitInput('');
      },
      handlePreview(file) {
        this.dialogVisible = true;
      },
      beforeUpload(file) {
        let _self = this;
        if(!this.useQiniu){
          //不使用qiniu不需要获取策略
          return true;
        }
        return new Promise((resolve, reject) => {
          token().then(response => {

            _self.dataObj.token = response.data.token;
            _self.dataObj.key = new Date().getTime()+file.name;
            _self.dataObj.serverUrl = response.data.serverUrl;
            _self.dataObj.bucketname = response.data.bucketname;
            resolve(true)
          }).catch(err => {
            console.log(err)
            reject(false)
          })
        })
      },
      handleUploadSuccess(res, file) {
        this.showFileList = true;
        this.fileList.pop();
        let url = this.dataObj.serverUrl + '/' + this.dataObj.key;
        if(!this.useQiniu){
          //不使用qiniu直接获取图片路径
          url = res.data.url;
        }
        this.fileList.push({name: file.name, url: url});
        this.emitInput(this.fileList[0].url);
      }
    }
  }
</script>
<style>

</style>


